import 'package:flutter_chat/common/service/services.dart';
import 'package:flutter_chat/common/utils/utils.dart';
import 'package:get/get.dart';

/// 自定义GetxController
class CustomGetxController extends GetxController {
  /// 是否自动关闭所有弹窗
  bool isAutoCloseLoading = true;

  /// 是否自动关闭所有吐司
  bool isAutoCloseToast = true;

  /// 是否自动删除WebSocket监听者
  bool isAutoRemoveWebSocketListener = true;

  /// WebSocket监听者
  Function(dynamic)? webSocketListener;

  @override
  void onInit() {
    super.onInit();
    if (webSocketListener != null) {
      // 添加WebSocket监听者
      WebSocketService.to.addListener(webSocketListener!);
    }
  }

  @override
  void onClose() {
    super.onClose();
    if (isAutoCloseLoading) {
      // 关闭所有弹窗
      LoadingUtil.close();
    }
    if (isAutoCloseToast) {
      // 关闭所有吐司
      ToastUtil.closeAllLoading();
    }
    if (isAutoRemoveWebSocketListener && webSocketListener != null) {
      // 删除WebSocket监听者
      WebSocketService.to.removeListener(webSocketListener!);
    }
  }
}
